const wx2my = require('../../wx2my');

// pages/person_detail/person_detail.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    changeName: true,
    changePhone: true,
    changeZHZX: true,
    changeAgain:true,
    inputPhone: '',
    inputName: '',
    avatar: '',
    account: ''
  },
  onLoad: function (options) {},
  onReady: function () {
    this.get_msg();
  },
  onShow: function () {},

  get_msg() {
    let that = this;
    app.wxRequest('profile', {
      'token': wx2my.getStorageSync('token')
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        that.setData({
          face_img: res.data.data.avatar,
          // avatar:res.data.data.avastar,
          name: res.data.data.nickname,
          account: res.data.data.account
        });
      }
    });
  },

  takePhone() {
    var that = this;
    wx2my.showActionSheet({
      itemList: ['拍照', '从相册中选择'],

      success(res) {
        console.log(res.tapIndex);

        if (res.tapIndex == 0) {
          wx2my.chooseImage({
            count: 1,
            // sizeType: ['compressed'],
            sourceType: ['camera'],
            success: function (res) {
              // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
              let tempFilePaths = res.tempFilePaths;
              let uploadImgCount = 0;

              for (let i = 0, h = tempFilePaths.length; i < h; i++) {
                wx2my.uploadFile({
                  url: app.globalData.host + '/user_api/upload',
                  filePath: tempFilePaths[i],
                  name: 'file',
                  formData: {},
                  header: {
                    'content-type': 'application/x-www-form-urlencoded'
                  },
                  success: function (res) {
                    let data = JSON.parse(res.data);
                    let resc = data.data.filename;
                    let file_url = data.data.file_url;
                    console.log(resc, file_url);
                    that.setData({
                      face_img: resc,
                      avatar: file_url
                    });
                  },
                  fail: function (res) {
                    wx2my.hideToast();
                    wx2my.showModal({
                      title: '错误提示',
                      content: '上传图片失败',
                      showCancel: false,
                      success: function (res) {}
                    });
                  }
                });
              }
            }
          });
        } else if (res.tapIndex == 1) {
          wx2my.chooseImage({
            count: 1,
            sourceType: ['album'],

            success(res) {
              // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
              let tempFilePaths = res.tempFilePaths;
              let uploadImgCount = 0;

              for (let i = 0, h = tempFilePaths.length; i < h; i++) {
                wx2my.uploadFile({
                  url: app.globalData.host + '/user_api/upload',
                  filePath: tempFilePaths[i],
                  name: 'file',
                  formData: {},
                  header: {
                    'content-type': 'application/x-www-form-urlencoded'
                  },
                  success: function (res) {
                    let data = JSON.parse(res.data);
                    let resc = data.data.filename;
                    let file_url = data.data.file_url;
                    console.log(resc, file_url);
                    that.setData({
                      face_img: resc,
                      avatar: file_url
                    });
                  },
                  fail: function (res) {
                    wx2my.hideToast();
                    wx2my.showModal({
                      title: '错误提示',
                      content: '上传图片失败',
                      showCancel: false,
                      success: function (res) {}
                    });
                  }
                });
              }
            }

          });
        }
      }

    });
  },

  zhzx() {
    // 账号注销
    this.setData({
      changeZHZX: false
    });
  },
  quedin2(){
    let that = this;
    app.wxRequest('cancelAccount', {
      'type': 3
    }, function (res) {
      if (res.data.code == 1) {
        wx2my.showModal({
          title: '账户注销',
          content: '注销成功！',
          showCancel: false,
          success: function (res) {}
        });
        wx2my.clearStorageSync();
        setTimeout(() => {
          wx2my.reLaunch({
            url: '/pages/home/home'
          });
        }, 2000);
      }
    });
  },
  quedin() {
    this.setData({      
      changeZHZX: true,
      changeAgain:false,
    });
  },
  cancle2(){
    this.setData({
      changePhone: true,
      changeZHZX: true,
      changeName: true,
      changeAgain:true,      
    });
  },
  userName(e) {
    console.log(e);
    let that = this;
    that.setData({
      inputName: e.detail.value
    });
  },

  userPhone(e) {
    console.log(e);
    let that = this;
    that.setData({
      inputPhone: e.detail.value
    });
  },

  name() {
    this.setData({
      changeName: false
    });
  },

  phone() {
    this.setData({
      changePhone: false
    });
  },

  cancle() {
    this.setData({
      changePhone: true,
      changeZHZX: true,
      changeName: true
    });
  },

  y_name() {
    let that = this;
    that.setData({
      name: that.data.inputName,
      changeName: true
    });
  },

  y_phone() {
    let that = this;
    that.setData({
      account: that.data.inputPhone,
      changePhone: true
    });
  },

  save() {
    let that = this;
    let data = {
      'token': wx2my.getStorageSync('token'),
      'nickname': that.data.name
    };
    if (that.data.account) data.account = that.data.account;
    if (that.data.avatar) data.avatar = that.data.avatar;
    console.log(data);
    app.wxRequest('edit', data, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        app.toast(res.data.msg);
        setTimeout(() => {
          wx2my.navigateBack();
        }, 2000);
      } else {
        app.toast(res.data.msg);
      }
    });
  },

  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {},
  onShareAppMessage: function () {}
});